package com.ersin.sistek.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ersin.sistek.dao.SiparisDao;
import com.ersin.sistek.model.BaseBean;
import com.ersin.sistek.model.SiparisBean;
import com.ersin.sistek.model.SiparisDetayBean;
import com.ersin.util.CustomHibernateDaoSupport;

@Repository("siparisDao")
public class SiparisDaoImpl extends CustomHibernateDaoSupport implements
		SiparisDao {

	@Override
	public void save(BaseBean baseBean) {
		getHibernateTemplate().save(baseBean);

	}

	@Override
	public void update(BaseBean baseBean) {
		getHibernateTemplate().update(baseBean);

	}

	@Override
	public void delete(BaseBean baseBean) {
		getHibernateTemplate().delete(baseBean);

	}

	@Override
	public List<SiparisDetayBean> findSiparisDetayById(int id) {
		@SuppressWarnings("unchecked")
		List<SiparisDetayBean> list = getHibernateTemplate().find(
				"from SiparisDetayBean where siparisId.id=?", id);
		return list;
	}

	@Override
	public List<BaseBean> getAll(int id) {
		@SuppressWarnings("unchecked")
		List<BaseBean> list = getHibernateTemplate().find("from SiparisBean where siparisKodu=?",id);
		return list;
	}

	@Override
	public List<SiparisBean> getSiparisAll(int siparisKoduIlk,
			int siparisKoduSon, Date ilkTar, Date sonTar) {
		@SuppressWarnings("unchecked")
		Session session = getHibernateTemplate().getSessionFactory()
				.openSession();
		Criteria criteria = session.createCriteria(SiparisBean.class);
		if (siparisKoduSon > 0)
			criteria.add(Restrictions.between("siparisKodu", siparisKoduIlk,
					siparisKoduSon));
		if (ilkTar != null && sonTar != null) {
			criteria.add(Restrictions.between("siparisTarihi", ilkTar, sonTar));
		}
		@SuppressWarnings("unchecked")
		List<SiparisBean> list = criteria.list();
		return list;
	}

	@Override
	public List<BaseBean> getAllMalzemeList() {
		@SuppressWarnings("unchecked")
		List<BaseBean> list = getHibernateTemplate().find("from MalzemeBean");
		return list;
	}

}
